﻿using Item.Buff;
using LitJson;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Battle;

namespace ItemComponent
{
    public class Area_Dmg : AreaItemComponent
    {
        protected const int AtkInterval = 100;
        protected const string Key_AreaDmg = "Dmg_Enter_Stay_Exit";

        [SerializeField,ReadOnly]
        protected int[] arrDmg;

        [SerializeField,ReadOnly]
        protected int[] arrCurDmg;

        protected List<ICanHitObj> listTarget=new List<ICanHitObj>();

        public override void InitComponent(JsonData data, Item.ItemBase item)
        {
            base.InitComponent(data,item);

            if (data[Key_AreaDmg] != null)
            {
                arrDmg = JsonMapper.ToObject<int[]>(data[Key_AreaDmg].ToJson());

                arrCurDmg = new int[arrDmg.Length];
                for (int i = 0; i < arrCurDmg.Length; i++)
                {
                    arrCurDmg[i] = arrDmg[i];
                }
            }

            UpdateAtkAsync();
        }

        public override void OnBuffChange(STBuffEffectData buffEffectData)
        {
            base.OnBuffChange(buffEffectData);

            arrCurDmg = new int[arrDmg.Length];
            for (int i = 0; i < arrCurDmg.Length; i++)
            {
                arrCurDmg[i] = (int)(arrDmg[i] * buffEffectData.fPower);
            }
        }

        protected async void UpdateAtkAsync()
        {
            ST_HitActionData hitActionData = new ST_HitActionData();
            while (!isDeath)
            {
                hitActionData.fAtk = AtkInterval / 1000f * arrCurDmg[1];
                for (int i = 0; i < listTarget.Count; i++)
                {
                    if (listTarget[i] == null || listTarget[i].IsDeath)
                    {
                        listTarget.RemoveAt(i);
                        i--;
                    }
                    else
                    {
                        listTarget[i].OnHit(hitActionData);
                    }
                }

                await Task.Delay(AtkInterval);
            }
        }

        public override void OnEnter2D(Collider2D target)
        {
            ICanHitObj canHitObj = target.GetComponentInParent<ICanHitObj>();
            if (canHitObj != null)
            {
                canHitObj.OnHit(new ST_HitActionData() { fAtk = arrCurDmg[0] });

                if (!canHitObj.IsDeath)
                    listTarget.Add(canHitObj);
            }
        }

        public override void OnExit2D(Collider2D target)
        {
            ICanHitObj canHitObj = target.GetComponentInParent<ICanHitObj>();
            if (canHitObj != null)
            {
                canHitObj.OnHit(new ST_HitActionData() { fAtk = arrCurDmg[2] });

                if (listTarget.Contains(canHitObj))
                    listTarget.Remove(canHitObj);
            }
        }

#if UNITY_EDITOR
        public static STMyComponetData GetMyComponetData(ST_AreaData areaData,int[] arrDmg)
        {
            var data =AreaItemComponent.GetMyComponetData(areaData);
            data.strType = typeof(Area_Dmg).FullName;
            data.data[Key_AreaDmg] = JsonMapper.ToObject(JsonMapper.ToJson(arrDmg));

            return data;
        }
#endif
    }
}
